package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.common.annotation.DataScope;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.utils.SecurityUtils;
import com.xjy.system.domain.SysContract;
import com.xjy.system.domain.bo.*;
import com.xjy.system.domain.vo.SysContractVo;
import com.xjy.system.mapper.SysContractMapper;
import com.xjy.system.service.*;
import com.xjy.system.utils.GeneralUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 合同基本信息Service业务层处理
 *
 * @author xjy
 * @date 2022-02-08
 */
@Service
public class SysContractServiceImpl extends ServicePlusImpl<SysContractMapper, SysContract> implements ISysContractService {

	@Autowired
	private ISysDeptService iSysDeptService;
	@Autowired
	private ISysProjService iSysProjService;
	@Autowired
	private ISysUserRoleService iSysUserRoleService;

	@Autowired
	private ISysImageMngService iSysImageMngService;

	@Override
	public SysContractVo queryById(Long id) {
		return getVoById(id, SysContractVo.class);
	}

	@DataScope(contract = "c")
	@Override
	public TableDataInfo<SysContractVo> queryPageList(SysContractQueryBo bo) {
		Long id = SecurityUtils.getLoginUser().getUser().getUserId();
		if (SecurityUtils.isAdmin(id)) {
			List<SysContractVo> list = baseMapper.queryPageSysContract(bo);
			return PageUtils.buildDataInfo(list);
		} else {
			List<SysContractVo> list1 = baseMapper.queryPageList(bo, id);
			return PageUtils.buildDataInfo(list1);
		}

	}

	@Override
	public List<SysContractVo> selectList(SysContractQueryBo bo) {
		bo.setAuditStatus(true);
		List<SysContractVo> list = baseMapper.queryPageSysContract(bo);
		return list;
	}

	@Override
	public List<SysContractVo> queryList(SysContractQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysContractVo.class);
	}

	private LambdaQueryWrapper<SysContract> buildQueryWrapper(SysContractQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysContract> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getNailName()), SysContract::getNailName, bo.getNailName());
		lqw.like(StrUtil.isNotBlank(bo.getBname()), SysContract::getBname, bo.getBname());
		lqw.like(StrUtil.isNotBlank(bo.getConstractName()), SysContract::getConstractName, bo.getConstractName());
		lqw.eq(StrUtil.isNotBlank(bo.getConstractNo()), SysContract::getConstractNo, bo.getConstractNo());
		lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
			SysContract::getSignTime, params.get("beginTime"), params.get("endTime"));//beginSignTime->beginTime
		lqw.eq(StrUtil.isNotBlank(bo.getConstractBigType()), SysContract::getConstractBigType, bo.getConstractBigType());
		lqw.eq(StrUtil.isNotBlank(bo.getConstractSmallType()), SysContract::getConstractSmallType, bo.getConstractSmallType());
		lqw.eq(bo.getUploadTime() != null, SysContract::getUploadTime, bo.getUploadTime());
		lqw.like(StrUtil.isNotBlank(bo.getContactsor()), SysContract::getContactsor, bo.getContactsor());
		lqw.like(StrUtil.isNotBlank(bo.getProjName()), SysContract::getProjName, bo.getProjName());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysContractAddBo bo) {
		SysContract add = BeanUtil.toBean(bo, SysContract.class);
		add.setUpdateTime(DateUtil.date());
		add.setId(GeneralUtils.getId(false));
		add.setProjName(iSysProjService.queryById(bo.getProjId()).getName());
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public synchronized Boolean updateByEditBo(SysContractEditBo bo) {
		SysContract update = BeanUtil.toBean(bo, SysContract.class);
		if (null != bo.getProjId()) {
			update.setProjName(iSysProjService.queryById(bo.getProjId()).getName());

		}
		validEntityBeforeSave(update);
		return updateById(update);
	}

	@Override
	public Boolean auditByEditBo(SysContractEditBo bo) {
		bo.setAuditStatus(true);
		boolean rsp = updateByEditBo(bo);
		if(rsp){
				this.uploadImage(bo);
		}
		return rsp;
	}
	private boolean uploadImage(SysContractEditBo bo){
		boolean rsp = false;
		if(StringUtils.isNotBlank(bo.getVoucher()) && !"[]".equals(bo.getVoucher())){
			SysImageMngAddBo sysImageMngAddBo = new SysImageMngAddBo();
			sysImageMngAddBo.setAuditStatus(true);
			sysImageMngAddBo.setProjId(bo.getProjId());
			sysImageMngAddBo.setImage(bo.getVoucher());
			sysImageMngAddBo.setType(2+"");
			sysImageMngAddBo.setOperatorId(SecurityUtils.getLoginUser().getUser().getUserId());
			sysImageMngAddBo.setOperator(SecurityUtils.getLoginUser().getUser().getUserName());
			sysImageMngAddBo.setOutNo(bo.getConstractNo());

			rsp = iSysImageMngService.insertByAddBo(sysImageMngAddBo);
		}

		return rsp;
	}
	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysContract entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}

	@Override
	public List<SysContract> listByProj(Long projId) {
		LambdaQueryWrapper<SysContract> lmd = Wrappers.lambdaQuery();
		lmd.select(SysContract::getId, SysContract::getConstractName).in(SysContract::getProjId, projId);
		return list(lmd);
	}
}
